package com.wlyuan.open.application.translator;

import com.wlyuan.core.domain.MappingIds;
import com.wlyuan.core.marker.MappingStatus;
import com.wlyuan.open.domain.api.OpenApi;
import com.wlyuan.open.domain.api.utils.MappingMethod;
import com.wlyuan.open.domain.api.utils.MappingOpenApi;
import com.wlyuan.open.vo.OpenApiDetailVO;
import com.wlyuan.open.vo.OpenClientApiVO;
import org.mapstruct.*;
import org.mapstruct.factory.Mappers;

import java.util.List;

/**
 * @author yuanzheng
 */
public class OpenApiTranslator {

    @Mapper(uses = {MappingIds.class, MappingStatus.class, MappingOpenApi.class, MappingMethod.class})
    public interface Translator {

        @Mappings({
                @Mapping(source = "status", target = "statusName"),
                @Mapping(source = "id", target = "apiId"),
        })
        OpenApiDetailVO translate(OpenApi source);

        List<OpenApiDetailVO> translate(List<OpenApi> source);

        @Mappings({
                @Mapping(source = "id", target = "apiId"),
        })
        OpenClientApiVO translateClientApi(OpenApi source);

        @AfterMapping
        default void after(@MappingTarget OpenClientApiVO target, OpenApi source) {
            target.setHasEnable(source.hasEnable());
        }

        List<OpenClientApiVO> translateClientApi(List<OpenApi> source);
    }

    private static final Translator TRANSLATOR = Mappers.getMapper(Translator.class);

    public static OpenApiDetailVO translate(OpenApi source) {
        return TRANSLATOR.translate(source);
    }

    public static List<OpenApiDetailVO> translate(List<OpenApi> source) {
        return TRANSLATOR.translate(source);
    }

    public static List<OpenClientApiVO> translateClientApi(List<OpenApi> source) {
        return TRANSLATOR.translateClientApi(source);
    }
}
